<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人中心</title>
    <style>
        body {
            background-color: #f2f2f2;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }

        .box {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
        }

        .box span {
            font-weight: bold;
        }

        .row {
            margin-bottom: 10px;
        }

        .opwindow {
            /*width: 100px;*/
            /*height: 100px;*/
            background-color: #fff;
            padding: 20px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            display: none;
        }

        .opwindow input {
            margin-bottom: 10px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        button.cancel {
            background-color: #ccc;
            margin-left: 10px;
        }
    </style>
</head>

<body>
<div class="container">
    <h1>个人中心</h1>
    <div class="box">
        <div class="row">
            <span>昵称：</span>
            <span id="username"></span>
        </div>
        <div class="row">
            <span>账号：</span>
            <span id="account"></span>
        </div>
        <div class="row">
            <span>余额：</span>
            <span id="money"></span>
            <button onclick="openWindow()">充值</button>
        </div>
        <div class="row">
            <span>积分：</span>
            <span id="score"></span>
        </div>
    </div>
    <div class="opwindow">
        <input id="balance_text" type="number" placeholder="请输入充值的金额">
        <button id="addBalanceSubmit">充值</button>
        <button onclick="offWindow()" class="cancel">取消</button>
    </div>
</div>

<script src="/admin/assets/js/jquery-3.5.1.min.js"></script>
<script th:inline="javascript">
    let user = [[${ session.user }]];
    console.log(user)
    init();
    // 渲染默认
    function init(){
        $('#username').text(user.userName);
        $('#account').text(user.account);
        $('#money').text(user.balance);
        $('#score').text(user.score);
    }

    // 打开充值窗口
    function openWindow(){
        $('.opwindow').show();
    }
    // 关闭充值窗口
    function offWindow(){
        $('#balance_text').val('');
        $('.opwindow').hide();
    }
    $('#addBalanceSubmit').on('click', function () {
        // 获取充值金额
        let number = $('#balance_text').val();
        console.log(number);
        if (number == null) {
            return
        }
        let userId = user.id
        if (addBalance(userId, number)) {
            let user = getUserById(user.id);
            $('#money').text(user.balance + Number(('#money').text()));
        } else {
            alert("充值失败！")
        }
    })

    // 充值金额 返回 true false
    function addBalance(userId, balance) {
        let flag = false;
        $.ajax({
            url: '/user.do/addBalance',
            type: 'put',
            async: false,
            data: {
                userId: userId,
                balance: balance
            },
            success: function (res) {
                console.log(res);
                if (res.code == 200) {
                    // 注册成功
                    flag = true
                    alert("充值成功")
                    $.ajax({
                        url:`/refreshSession/${userId}`,
                        type:'get',
                        success:function () {
                            location.reload();
                        }

                    })
                }
            }
        })
        return flag
    }
    // 根据 用户 id 得到 用户信息
    function getUserById(userId) {
        let user = {};
        $.ajax({
            url: '/user.do/selectUser',
            type: 'post',
            async: false,
            data: {
                userId: userId
            },
            success: function (res) {
                console.log(res);
                if (res.code == 200) {
                    user = res.data
                }
            }
        })
        return user;
    }
</script>
<script>
</script>
</body>

</html>